package com.kinth.work.tyc;

import java.util.ArrayList;

import com.kinth.dbutil.ImplDAO;

/**
 * 
 * <p>Title: 慧聪网的数据库工具类</p>
 * <p>Description: 
 * 主要是查和增。主要和ImplDAO交互。
 * </p>
 * @author 23192
 * @2017年3月28日
 */
public class dbUtil
{

	public static boolean insertUrl(TYCurl huiCong, String tableName)
	{
		System.out.println("测试urlr:"+huiCong.getUrl());
		String[] params = {huiCong.getUrl(),huiCong.getLocation()};
		ImplDAO implDAO = new ImplDAO();
		String sql = "insert into `"+tableName+"` (`url`,`location`)values(?,?)";
		try
		{
			return implDAO.doExecute(sql, params);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("重复插入");
		}
		//System.out.println("可以");
		return false;
	}

	
	public static ArrayList<TYCurl> queryUrl(String tableName, int row,int numOnce)
	{
		ArrayList<TYCurl> result = new ArrayList<TYCurl> ();
		ArrayList<String[]> all = new ArrayList<String[]>();
		String sql ="select * from "+tableName+" limit "+row+","+numOnce;
		ImplDAO dao = new ImplDAO();
		try {
			all = dao.doQuery(sql, null);
			for(int i = 0; i < all.size(); i++)
			{
				String []sTemp = all.get(i);
				TYCurl url = new TYCurl();
				url.setUrl(sTemp[0]);
				url.setLocation(sTemp[1]);
				result.add(url);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("重复插入");
		}
		return result;
	}
	
	//根据url获得industry
	public static ArrayList<String> queryIndustry(String tableName,String key)
	{
		ArrayList<String> result = new ArrayList<String> ();
		ArrayList<String[]> all = new ArrayList<String[]>();
		String sql ="select industry from "+tableName+ " where url = '" +key+"';";
		ImplDAO dao = new ImplDAO();
		try {
			all = dao.doQuery(sql, null);
			for(int i = 0; i < all.size(); i++)
			{
				String []sTemp = all.get(i);
				//UrlHuiCong url = new UrlHuiCong();
				//url.setUrl(sTemp[0]);
				//url.setIndustry(sTemp[1]);
				result.add(sTemp[0]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args)
	{
		ArrayList<TYCurl> urlList = dbUtil.queryUrl("huicong_url1", 0, 5);
		System.out.println("dbutil： "+urlList.size());
	}
}
